﻿using SuperJoe.Framework.Common;
using SuperJoe.Framework.Core;
using SuperJoe.Framework.Core.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WWX.Admin.IDAL;
using WWX.Model;

namespace WWX.Admin.BLL
{
    public class UserBLL : BaseBLL<IUserDAL>
    {
        public void Add(User user)
        {
            user.Id = Guid.NewGuid().ToString();
            user.CreatedBy = SuperJoeContext.Current.UserId;
            user.CreatedTime = DateTime.Now;
            user.UpdatedTime = DateTime.Now;
            user.UpdateBy = SuperJoeContext.Current.UserId;
            user.Pwd = MD5.Encrypt3DES(user.Pwd);
            this.dal.Add(user);
        }

        public User Get(string account, string pwd)
        {
            return this.dal.Get(account, MD5.Encrypt3DES(pwd));
        }

        public void UpdatePwd(string account, string oldPwd, string newPwd)
        {
            var encrypedPwd = MD5.Encrypt3DES(oldPwd);

            var member = this.dal.Get(account, encrypedPwd);
            if (member == null)
            {
                ThrowHelper.ThrowFriendlyException("密码错误");
            }

            member.Pwd = MD5.Encrypt3DES(newPwd);
            member.UpdateBy = SuperJoeContext.Current.UserId;
            member.UpdatedTime = DateTime.Now;

            this.dal.Update(member);
        }

        public void UpdatePwd(string id, string pwd)
        {
            var encrypedPwd = MD5.Encrypt3DES(pwd);
            var member = this.dal.Get(id);
            member.Pwd = encrypedPwd;
            member.UpdateBy = SuperJoeContext.Current.UserId;
            member.UpdatedTime = DateTime.Now;
            this.dal.Update(member);
        }

        public void Update(User user)
        {
            var dbUser = this.dal.Get(user.Id);
            dbUser.IsActive = user.IsActive;
            dbUser.Remark = user.Remark;
            this.dal.Update(dbUser);
        }

        public List<User> GetList()
        {
            return this.dal.GetList();
        }
    }
}
